import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:series_play/comm/ext/context_ext.dart';
import 'package:series_play/generated/assets.dart';
import 'package:series_play/pages/bingewatch/binge_watch_list_page.dart';
import 'package:series_play/pages/bingewatch/controller/binge_watch_controller.dart';
import 'package:series_play/pages/bingewatch/watch_history_page.dart';
import 'package:series_play/widget/app_button.dart';
import 'package:series_play/widget/app_color.dart';
import 'package:series_play/widget/app_tab.dart';
import 'package:series_play/widget/app_image.dart';
import 'package:series_play/widget/app_text.dart';

import '../../comm/base/base_state.dart';

/// 追剧
///
/// 追剧、播放历史页面
class BingeWatchPage extends StatefulWidget {
  const BingeWatchPage({super.key});

  @override
  State<BingeWatchPage> createState() => _BingeWatchPageState();
}

class _BingeWatchPageState
    extends BaseControllerState<BingeWatchPage, BingeWatchController>
    with TickerProviderStateMixin {
  late TabController _tabController;

  late BingeWatchListPage _bingeWatchListPage;

  late WatchHistoryPage _watchHistoryPage;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
    // 子页面不能立马find 有延迟
    _bingeWatchListPage = BingeWatchListPage(mController);
    _watchHistoryPage = WatchHistoryPage(mController);
  }

  @override
  Widget buildPage(BuildContext context) {
    return Column(
      children: [
        SizedBox(
          width: 1.sw,
          child: Stack(
            children: [
              Align(
                child: AppTab.bingWatch([
                  context.tr.tabBingeWatch,
                  context.tr.playHistory,
                ], _tabController),
              ),
              Positioned(
                right: 16.w,
                bottom: 0,
                top: 0,
                child: Row(
                  children: [
                    Obx(() {
                      return mController.isEditModel.value
                          ? GestureDetector(
                              onTap: () {
                                mController.isEditModel.value = false;
                              },
                              child: Container(
                                height: 24.w,
                                padding: const EdgeInsets.symmetric(
                                  horizontal: 12,
                                ).w,
                                decoration: BoxDecoration(
                                  color: Colors.transparent,
                                  borderRadius: BorderRadius.circular(4).r,
                                  border: Border.all(
                                    width: 0.3.w,
                                    color: AppColor.white_40,
                                  ),
                                ),
                                alignment: Alignment.center,
                                child: AppText(
                                  '取消',
                                  color: AppColor.white_40,
                                  fontSize: 12,
                                ),
                              ),
                            )
                          : AppImage.assetsWith18(
                              Assets.imagesIcEditWhite,
                              onTap: () {
                                mController.isEditModel.value = true;
                              },
                            );
                    }),
                  ],
                ),
              ),
            ],
          ),
        ),
        10.verticalSpace,
        Expanded(
          child: TabBarView(
            controller: _tabController,
            children: [_bingeWatchListPage, _watchHistoryPage],
          ),
        ),

        // 编辑删除 取消全选
        Obx(() {
          return Visibility(
            visible: mController.isEditModel.value,
            child: Container(
              padding: const EdgeInsets.symmetric(
                horizontal: 18,
                vertical: 6,
              ).w,
              decoration: BoxDecoration(
                border: Border(
                  top: BorderSide(width: 0.1.w, color: AppColor.white_40),
                ),
              ),
              child: Row(
                children: [
                  Expanded(
                    child: AppButton.normal(name: '取消全选', onTap: () {}),
                  ),
                  8.horizontalSpace,
                  Expanded(
                    child: AppButton.normal(name: '删除', onTap: () {}),
                  ),
                ],
              ),
            ),
          );
        }),
      ],
    );
  }

  @override
  BingeWatchController createController() {
    return BingeWatchController();
  }

  @override
  bool hideAppbar() {
    return true;
  }

  @override
  bool showDefaultBackground() {
    return true;
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }
}
